import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import fs from 'fs'

export default defineConfig(({ command }) => {
  return {
    plugins: [
      react(),
      // 自定义插件来提供mock文件
      {
        name: 'mock-static-files',
        configureServer(server) {
          server.middlewares.use('/mock', (req, res, next) => {
            const filePath = path.join(__dirname, 'mock', req.url || '')
            
            // 检查文件是否存在
            if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
              // 设置正确的Content-Type
              if (filePath.endsWith('.json')) {
                res.setHeader('Content-Type', 'application/json')
              }
              
              // 读取并返回文件内容
              const content = fs.readFileSync(filePath, 'utf-8')
              res.end(content)
            } else {
              next()
            }
          })
        }
      }
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@/shared': path.resolve(__dirname, './src/shared'),
        '@/mall': path.resolve(__dirname, './src/projects/mall'),
        '@/admin': path.resolve(__dirname, './src/projects/admin')
      }
    },
    server: {
      port: 3000,
      host: true,
      open: true,
      fs: {
        allow: ['..']
      }
    },
    publicDir: 'public',
    build: {
      outDir: 'dist',
      rollupOptions: {
        input: {
          main: path.resolve(__dirname, 'index.html')
        }
      }
    }
  }
}) 